package com.learn.algorithm;

/**
 * 求最大公约数和最小公倍数
 */
public class GCDLCM1 {
    public static void main(String[] args) {
        //todo 最大公约数
        System.out.println(get_gcd(337,319));
        //todo 最小公倍数
        System.out.println(get_lcm(337,319));

    }
    //最大公约数
    public static int get_gcd(int n1, int n2) {
        int gcd = 0;
        if (n1<n2) {
            n1 = n1+n2;
            n2 = n1-n2;
            n1 = n1-n2;
        }
        if (n1%n2 == 0) {
            gcd = n2;
        }
        while(n1%n2 > 0 ) {
            n1 = n1%n2;
            if (n1<n2) {
                n1 = n1+n2;
                n2 = n1-n2;
                n1 = n1-n2;
            }
            if (n1%n2 == 0) {
                gcd = n2;
            }
        }
        return gcd;
    }

    //最小公倍数
    public static int get_lcm(int n1, int n2) {
        return n1*n2 / get_gcd(n1,n2);
    }

}
